/**
 * @(#)FpgzVillageController.java 2017年7月29日
 * 
 * Copyright 2000-2017 by ChinanetCenter Corporation.
 *
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * ChinanetCenter Corporation ("Confidential Information").  You
 * shall not disclose such Confidential Information and shall use
 * it only in accordance with the terms of the license agreement
 * you entered into with ChinanetCenter.
 * 
 */

package com.teamall.web.fpgz.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.teamall.core.common.JModelAndView;
import com.teamall.core.common.Page;
import com.teamall.framework.exception.BaseAppException;
import com.teamall.framework.log.LogManager;
import com.teamall.framework.util.DateUtils;
import com.teamall.web.fpgz.db.po.CodeFamilyAidLogPO;
import com.teamall.web.fpgz.db.po.CodeFamilyAndAidstaffByVillagePO;
import com.teamall.web.fpgz.db.po.CodeFamilyIncomePO;
import com.teamall.web.fpgz.db.po.CodeFamilyInfoPO;
import com.teamall.web.fpgz.db.po.CodeFamilyMemberPO;
import com.teamall.web.fpgz.db.po.CodeFamilyVistedLogPO;
import com.teamall.web.fpgz.db.po.CodeVillageInfoPO;
import com.teamall.web.fpgz.db.po.CodeVillageProjectInfoPO;
import com.teamall.web.fpgz.db.po.CodeVillageTeamMemberPO;
import com.teamall.web.fpgz.service.ICodeFamilyAidLogService;
import com.teamall.web.fpgz.service.ICodeFamilyAndAidstaffByVillageService;
import com.teamall.web.fpgz.service.ICodeFamilyIncomeService;
import com.teamall.web.fpgz.service.ICodeFamilyInfoService;
import com.teamall.web.fpgz.service.ICodeFamilyMemberService;
import com.teamall.web.fpgz.service.ICodeFamilyVistedLogService;
import com.teamall.web.fpgz.service.ICodeVillageInfoService;
import com.teamall.web.fpgz.service.ICodeVillageProjectInfoService;
import com.teamall.web.fpgz.service.ICodeVillageTeamMemberService;
import com.teamall.web.sjtj.db.po.CodeVisitedAidRankPO;
import com.teamall.web.sjtj.service.ICodeVisitedAidRankService;
import com.teamall.web.sjtj.service.ICodeVisitedOrderService;

/**扶贫跟踪-村级别信息
 * @author pan.xiaobo
 * @date 2017年7月29日
 * @version $Revision$
 */
@Controller
@RequestMapping("/fpgzvillage")
public class FpgzVillageController {

	private static final LogManager logger = LogManager.getLogger(FpgzVillageController.class);

	@Autowired
	private ICodeVillageInfoService codeVillageInfoService;

	@Autowired
	private ICodeVillageTeamMemberService codeVillageTeamMemberService;

	@Autowired
	private ICodeFamilyInfoService codeFamilyInfoService;

	@Autowired
	private ICodeFamilyAidLogService codeFamilyAidLogService;

	@Autowired
	private ICodeFamilyVistedLogService codeFamilyVistedLogService;

	//家庭成员
	@Autowired
	private ICodeFamilyMemberService codeFamilyMemberService;

	@Autowired
	private ICodeFamilyIncomeService codeFamilyIncomeService;

	@Autowired
	private ICodeVillageProjectInfoService codeVillageProjectInfoService;

	@Autowired
	private ICodeFamilyAndAidstaffByVillageService codeFamilyAndAidstaffByVillageService;

	@Autowired
	private ICodeVisitedOrderService codeVisitedOrderService;
	
	@Autowired
	private ICodeVisitedAidRankService codeVisitedAidRankService;
	
	/**
	 * 村-扶贫跟踪首页
	 * @param villageId
	 * @return
	 * @throws BaseAppException
	 */
	@RequestMapping("/index")
	public ModelAndView index(int villageId) throws BaseAppException {
		CodeVillageInfoPO villageInfo = codeVillageInfoService.selectByPrimaryKey(villageId);
		CodeVillageTeamMemberPO qryTeamMemberPO = new CodeVillageTeamMemberPO();
		qryTeamMemberPO.setVillageId(villageId);
		List<CodeVillageTeamMemberPO> teamMemberList = codeVillageTeamMemberService
				.selectByArg(qryTeamMemberPO);
		CodeFamilyAndAidstaffByVillagePO qryFamilyAidstaffPO = new CodeFamilyAndAidstaffByVillagePO();
		qryFamilyAidstaffPO.setVillageId(villageId);
		List<CodeFamilyAndAidstaffByVillagePO> familyAidstaffList = codeFamilyAndAidstaffByVillageService
				.selectByArg(qryFamilyAidstaffPO);

		//帮扶排名
//		CodeVisitedOrderPO qryVisitedOrder = new CodeVisitedOrderPO();
//		qryVisitedOrder.setVillageId(villageId);
//		List<CodeVisitedOrderPO> visitedOrderList = codeVisitedOrderService.selectByArg(qryVisitedOrder);
//		List<CodeVisitedOrderPO> visitedOrderResult = new ArrayList<CodeVisitedOrderPO>();
//		if(null!=visitedOrderList&&visitedOrderList.size()>0){
//		    visitedOrderResult = visitedOrderList.subList(0, visitedOrderList.size()>5?5:visitedOrderList.size());
//		}
		
		CodeVisitedAidRankPO qryVisitedAidRankPO = new CodeVisitedAidRankPO();
		qryVisitedAidRankPO.setVillageId(villageId);
		qryVisitedAidRankPO.setVtime(DateUtils.date2String(new Date(), DateUtils.STR_DATE_FORMAT_YEAR));
		List<CodeVisitedAidRankPO> visitedAidRankList = codeVisitedAidRankService.selectByArg(qryVisitedAidRankPO);
		List<CodeVisitedAidRankPO> visitedOrderResult = new ArrayList<CodeVisitedAidRankPO>();
		if(null!=visitedAidRankList&&visitedAidRankList.size()>0){
		    visitedOrderResult = visitedAidRankList.subList(0, visitedAidRankList.size()>5?5:visitedAidRankList.size());
		}
		
		
		ModelAndView modelAndView = new JModelAndView("/fpgz/fpgz_village");
		modelAndView.addObject("villageInfo", villageInfo);
		modelAndView.addObject("teamMemberList", teamMemberList);
		modelAndView.addObject("teamMemberNum", null==teamMemberList?0:teamMemberList.size());
		modelAndView.addObject("visitedOrderResult", visitedOrderResult);
		if (null != familyAidstaffList && familyAidstaffList.size() > 0) {
			modelAndView.addObject("familyAidstaff", familyAidstaffList.get(0));
		}

		return modelAndView;
	}

	/**
	 * 获取工作队队员列表
	 * @param qryRecord
	 * @param resultPage
	 * @return
	 * @throws BaseAppException
	 */
	@ResponseBody
	@RequestMapping("/qryVillageTeam")
	public Page<CodeVillageTeamMemberPO> qryVillageTeam(CodeVillageTeamMemberPO qryRecord,
			Page<CodeVillageTeamMemberPO> resultPage) throws BaseAppException {
		//查詢条件：村id
		resultPage = codeVillageTeamMemberService.selectByArgAndPage(qryRecord, resultPage);
		return resultPage;
	}

	/**
	 * 查询帮扶项目列表
	 * @param qryRecord
	 * @param resultPage
	 * @return
	 * @throws BaseAppException
	 */
	@ResponseBody
	@RequestMapping("/qryVillageProjectInfo")
	public Page<CodeVillageProjectInfoPO> qryFamilyAidLog(CodeVillageProjectInfoPO qryRecord,
			Page<CodeVillageProjectInfoPO> resultPage) throws BaseAppException {
		//查詢条件：村id,贫困户名等
		resultPage = codeVillageProjectInfoService.selectByArgAndPage(qryRecord, resultPage);
		return resultPage;
	}

	/**
	 * 查询帮扶记录列表
	 * @param qryRecord
	 * @param resultPage
	 * @return
	 * @throws BaseAppException
	 */
	@ResponseBody
	@RequestMapping("/qryFamilyAidLog")
	public Page<CodeFamilyAidLogPO> qryFamilyAidLog(CodeFamilyAidLogPO qryRecord,
			Page<CodeFamilyAidLogPO> resultPage) throws BaseAppException {
		//查詢条件：村id,贫困户名等
		resultPage = codeFamilyAidLogService.selectByArgAndPage(qryRecord, resultPage);
		return resultPage;
	}

	/**
	 * 查询走访日志记录列表
	 * @param qryRecord
	 * @param resultPage
	 * @return
	 * @throws BaseAppException
	 */
	@ResponseBody
	@RequestMapping("/qryFamilyVisitedLog")
	public Page<CodeFamilyVistedLogPO> qryFamilyVisitedLog(CodeFamilyVistedLogPO qryRecord,
			Page<CodeFamilyVistedLogPO> resultPage) throws BaseAppException {
		//查詢条件：村id,贫困户名等
		resultPage = codeFamilyVistedLogService.selectByArgAndPage(qryRecord, resultPage);
		return resultPage;
	}

	/**
	 * 查询贫困户列表
	 * @param qryRecord
	 * @param resultPage
	 * @return
	 * @throws BaseAppException
	 */
	@ResponseBody
	@RequestMapping("/qryFamilyInfo")
	public Page<CodeFamilyInfoPO> qryFamilyInfo(CodeFamilyInfoPO qryRecord,
			Page<CodeFamilyInfoPO> resultPage) throws BaseAppException {
		//查詢条件：村id,贫困户名等
		resultPage = codeFamilyInfoService.selectByArgAndPage(qryRecord, resultPage);
		return resultPage;
	}

	/**
	 * 村-贫困户详情
	 * @param villageId
	 * @param familyId
	 * @return
	 * @throws BaseAppException
	 */
	@RequestMapping("/familyDetail")
	public ModelAndView familyDetail(int villageId, int familyId) throws BaseAppException {
		// 贫困户家庭信息
		Page<CodeFamilyInfoPO> familyInfos = new Page<CodeFamilyInfoPO>();
		CodeFamilyInfoPO qryFI = new CodeFamilyInfoPO();
		qryFI.setVillageId(villageId);
		qryFI.setFamilyId(familyId);
		codeFamilyInfoService.selectByArgAndPage(qryFI, familyInfos);

		//家庭成员列表
		CodeFamilyMemberPO qryFM = new CodeFamilyMemberPO();
		qryFM.setFamilyId(familyId);
		List<CodeFamilyMemberPO> familyMemberList = codeFamilyMemberService.selectByArg(qryFM);

		ModelAndView modelAndView = new JModelAndView("/fpgz/fpgz_village_pkh_detail");
		// 贫困户家庭信息
		if (familyInfos.getResultList().size() > 0) {
			modelAndView.addObject("familyInfo", familyInfos.getResultList().get(0));
			//计划脱贫时间
			modelAndView.addObject("planShakeOffDate",
					DateUtils.date2String(familyInfos.getResultList().get(0).getPlanShakeOffDate(),
							DateUtils.STR_DATE_FORMAT_YEAR));
		}
		else {
			modelAndView.addObject("familyInfo", new CodeFamilyInfoPO());
			//计划脱贫时间
			modelAndView.addObject("planShakeOffDate", "");
		}

		modelAndView.addObject("familyMemberList", familyMemberList);
		return modelAndView;
	}

	/**
	 * 查询收入情况列表
	 * @param qryRecord
	 * @param resultPage
	 * @return
	 * @throws BaseAppException
	 */
	@ResponseBody
	@RequestMapping("/qryFamilyIncome")
	public Page<CodeFamilyIncomePO> qryFamilyIncome(CodeFamilyIncomePO qryRecord,
			Page<CodeFamilyIncomePO> resultPage) throws BaseAppException {
		//查詢条件：村id,贫困户名等
		resultPage = codeFamilyIncomeService.selectByArgAndPage(qryRecord, resultPage);
		return resultPage;
	}
}
